package com.wl.org.service.impl;

import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;
import com.wl.basic.exception.BusinessException;
import com.wl.basic.util.BaiduAuditUtils;
import com.wl.basic.util.Md5Utils;
import com.wl.basic.util.StrUtils;
import com.wl.org.domain.Employee;
import com.wl.org.domain.Shop;
import com.wl.org.domain.ShopAuditLog;
import com.wl.org.dto.ShopDto;
import com.wl.org.mapper.EmployeeMapper;
import com.wl.org.mapper.ShopAuditLogMapper;
import com.wl.org.mapper.ShopMapper;
import com.wl.org.service.IShopService;
import com.wl.basic.service.impl.BaseServiceImpl;
import com.wl.user.domain.Logininfo;
import com.wl.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.internet.MimeMessage;
import java.util.List;


/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired//注入java邮箱对象
   /* private JavaMailSender javaMailSender;*/
    private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;
    //店铺入驻
    @Override
    @Transactional
    public void settlement(Shop shop) {
        //1.判断null字符
        if(StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺名称不能为空");
        }
        //2.查询店铺名称和地址
        Shop shop1 = shopMapper.loadByNameAndAddress(shop);

        //百度AI智能审核
        Boolean boo1 = BaiduAuditUtils.TextCensor(shop.getName() + shop.getAddress());
        if(!boo1){
            throw  new BusinessException("店铺名称和地址不合法");
        }
        if(!StringUtils.isEmpty(shop.getLogo())){
            String url = "http://123.207.27.208"+shop.getLogo(); //常量
            Boolean boo2 = BaiduAuditUtils.ImgCensor(url);
            if(!boo2){
                throw  new BusinessException("logo违规");
            }
        }

        //4.获取店铺管理员信息
        Employee admin = shop.getAdmin();
        //4.1添加到员工表
        //设置盐值
        String complexRandomString = StrUtils.getComplexRandomString(32);
        admin.setSalt(complexRandomString);
        //设置密码
        String s = Md5Utils.encrypByMd5(admin.getPassword() + complexRandomString);
        admin.setPassword(s);

        //保存logininfo的数据
        Logininfo logininfo = new Logininfo();
        //使用工具类拷贝
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);
        logininfoMapper.save(logininfo);

        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.save(admin);

        //5.给店铺管理员id设置为员工的id
        shop.setAdminId(admin.getId());
        //添加到shop表中
        shopMapper.save(shop);

        //6.给employee的shopID赋值等于shop表的id
        admin.setShop_id(shop.getId());
        //7.更新employee表
        employeeMapper.update(admin);

    }

    /**
     * 驳回
     * @param shopAuditLog
     * @throws MessagingException
     */
    @Override
    @Transactional
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException, javax.mail.MessagingException {
        //1.修改状态
        //1.1先查询一个状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(4);//设置
        shopMapper.update(shop);//修改shop
        //2.添加审核日志
        shopAuditLog.setState(4);//设置状态
        shopAuditLog.setAuditId(350L);//设置审核人
        shopAuditLogMapper.save(shopAuditLog);//重新添加到表里面
        //3.发送激活邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();//获取java邮箱
        //
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //设置发见人
        mimeMessageHelper.setFrom("2026029753@qq.com");
        //设置接收人
        //动态获取
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        mimeMessageHelper.setTo(employee.getEmail());
        //S设置主题
        mimeMessageHelper.setSubject("店铺激活邮件!!!");
        //设置内容
        mimeMessageHelper.setText("<h1>店铺激活失败!!!</h1><h3><a href='http://localhost:8081/#/register'>点击该链接重新入驻</a></h3>",true);
        javaMailSender.send(mimeMessage);

    }

    /**
     * 通过
     * @param shopAuditLog
     * @throws MessagingException
     */
    @Override
    @Transactional
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException, javax.mail.MessagingException {
        //1.修改状态
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);//设置
        shopMapper.update(shop);//修改shop
        //2.添加审核日志
        shopAuditLog.setState(2);//设置状态
        shopAuditLog.setAuditId(350L);//设置审核人
        shopAuditLogMapper.save(shopAuditLog);//重新添加到表里面
        //3.发送激活邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();//获取java邮箱
        //
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //设置发见人
        mimeMessageHelper.setFrom("2026029753@qq.com");
        //设置接收人
        //动态获取
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        mimeMessageHelper.setTo(employee.getEmail());
        //S设置主题
        mimeMessageHelper.setSubject("店铺激活邮件!!!");
        //设置内容
        mimeMessageHelper.setText("<h1>店铺激活邮件成功!!!</h1><h3><a href='http://localhost:8080/shop/active/"+shopAuditLog.getShopId()
                +"'>点击该链接激活店铺</a></h3>",true);
        javaMailSender.send(mimeMessage);

    }
    //直接调mapper层
    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}
